/**
 * @file ApplicationLayer.h
 * @Synopsis
 * @author Mingyu Wu
 * @version 1.0.0
 * @date 2024-04-22
 */
#ifndef _APPLICATION_LAYER_H_
#define _APPLICATION_LAYER_H_

#include "../Configuration.h"
#include "../DataBase.h"
#include "../Thread.h"
#include "../../Util.h"
#include <algorithm>
#include <chrono>
#include <functional>
#include <iostream>
#include <memory>
#include <set>
#include <string>
#include <thread>
#include <unistd.h>
#include <utility>
#include <vector>
#include <xxhash.h>

/**
 * @Synopsis  对上层接口
 */
class ApplicationLayer {
  public:
    /**
     * @Synopsis 应用层构造函数
     */
    ApplicationLayer();

    virtual ~ApplicationLayer();

    /**
     * @Synopsis 查询数据
     *
     * @Param readData 将需要查询数据填入 map的key中
     *
     * @return 成功返回 ErrorInfo::OK, 失败返回功能码
     */
    int QueryData(std::unordered_map<std::string, std::string> &readData);

    /**
     * @Synopsis 下发控制数据
     *
     * @Param whichCCU 下发给哪个CCU， "CCU1", "CCU2", "CCU3"
     * @Param writeData  所需下发的控制数据
     *
     * @return 成功返回ErrorInfo::OK, 失败返回错误码
     */
    int SendControlData(
        std::string                                       whichCCU,
        std::vector<std::pair<std::string, std::string>> &writeData);

    /**
     * @Synopsis 构造类初始化
     *
     * @return 成功返回 ErrorInfo::OK, 失败返回错误码
     */
    int Init();

  private:
    int CommitQuery(std::unordered_map<std::string, std::string> &readData);

  private:
    std::vector<std::unordered_map<std::string, std::string>> hashTab;

    std::set<int> hashGroupSet;
    //          HashKey         Fields
    std::map<std::string, std::unordered_map<std::string, std::string>>
        StoreQueryFields;

    std::shared_ptr<DataBase> db;
};

#endif // !_APPLICATION_LAYER_H_
